package com.yuhanginfo.modules.web.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.WebCrudService;
import com.yuhanginfo.common.utils.ReadProperties;
import com.yuhanginfo.modules.web.entity.WebSms;
import com.yuhanginfo.modules.web.util.SmsMobile;
import com.yuhanginfo.modules.web.dao.WebSmsDao;

/**
 * 地址管理Service
 * @author lvls
 * @version 2017-10-24
 */
@Service
@Transactional(readOnly = true)
public class WebSmsService extends WebCrudService<WebSmsDao, WebSms> {
	
	@Autowired
	WebSmsDao webSmsDao;
	
	public WebSms get(String id) {
		return super.get(id);
	}
	
	public List<WebSms> findList(WebSms sms) {
		return super.findList(sms);
	}
	
	public Page<WebSms> findPage(Page<WebSms> page, WebSms sms) {
		return super.findPage(page, sms);
	}
	
	@Transactional(readOnly = false)
	public void save(WebSms sms) {
		super.save(sms);
	}
	
	@Transactional(readOnly = false)
	public void delete(WebSms sms) {
		super.delete(sms);
	}
	
	@Transactional(readOnly = false)
	public void getÇode(String mobile){
		Date now = new Date();
		//生成6位随机数
		int code = (int)((Math.random()*9+1)*100000);
		//根据手机号查询验证码
		WebSms querySms = new WebSms();
		querySms.setPhone(mobile);
		WebSms resultSms = webSmsDao.getCodeByPhone(querySms);
		if(resultSms == null){
			WebSms entity = new WebSms();
			entity.setCode(String.valueOf(code));
			entity.setPhone(mobile);
			entity.setCreateDate(now);
			entity.setUpdateDate(now);
			webSmsDao.insert(entity);
		}else{
			WebSms entity = new WebSms();
			entity.setCode(String.valueOf(code));
			entity.setPhone(mobile);
			entity.setUpdateDate(now);
			webSmsDao.updateByPhone(entity);
		}
		//发送短信
		SmsMobile.sendMessage(mobile, ReadProperties.ReadByName("SMS_CODE"),"#code#="+code );
	}
	

	public Map<String,Object> checkÇode(String mobile,String code){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("resultCode", 1);
		//根据手机号查询
		WebSms querySms = new WebSms();
		querySms.setPhone(mobile);
		WebSms resultSms = webSmsDao.getCodeByPhone(querySms);
		if(resultSms == null){
			resultMap.put("resultCode", 0);
			resultMap.put("resultMessage", "验证码错误");
			return resultMap;
		}
		//匹配验证码
		if(!code.equals(resultSms.getCode())){
			resultMap.put("resultCode", 0);
			resultMap.put("resultMessage", "验证码错误");
			return resultMap;
		}
		//查看验证码是否过期
		Long now = new Date().getTime();
		Long createTime = resultSms.getUpdateDate().getTime();
		if( (now - createTime) > 180000 ){
			resultMap.put("resultCode", 0);
			resultMap.put("resultMessage", "验证码已失效");
			return resultMap;
		}
		return resultMap;
	}
	
	public boolean checkÇode2(String mobile,String code){
		//根据手机号查询
		WebSms querySms = new WebSms();
		querySms.setPhone(mobile);
		WebSms resultSms = webSmsDao.getCodeByPhone(querySms);
		if(resultSms == null){
			return false; 
		}
		//匹配验证码
		if(!code.equals(resultSms.getCode())){
			return false;
		}
		//查看验证码是否过期
		Long now = new Date().getTime();
		Long createTime = resultSms.getUpdateDate().getTime();
		if( (now - createTime) > 180000 ){
			return false;
		}
		return true;
	}
}